Python - How can I aggregate a pandas dataframe base on conditions on different rows

join shbcf.ru